package com.atguigu4.search_sort.exer2;

/**
 * @author lhl
 * @date 2025/3/31 14:26
 */
public class BinarySearchTest {

    public static void main(String[] args) {
        int[] arr2 = new int[]{2,4,5,8,12,15,19,26,37,49,51,66,89,100};

        int target = 5;

        int head = 0; //首索引
        int end = arr2.length - 1; //尾索引
        boolean isFlag = false;

        while (head <= end){

            int middle = (head + end) / 2;

            if (target == arr2[middle]){
                System.out.println("找到了" + target + ",对应的位置为："+middle);
                isFlag = true;
                break;
            }else if (target > arr2[middle]){
                head = middle + 1;
            }else{
                end = middle - 1;
            }
        }

        if (!isFlag){
            System.out.println("不好意思，没找到对应的元素");
        }
    }
}
